package com.example.city_home_app.RecyclerViewAdapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.city_home_app.Activity.BaseActivity;
import com.example.city_home_app.Activity.MomentInfoActivity;
import com.example.city_home_app.Activity.UserInfoActivity;
import com.example.city_home_app.Activity.VideoActivity;
import com.example.city_home_app.Bean.Down_Moment;
import com.example.city_home_app.Bean.Down_Universe;
import com.example.city_home_app.Bean.MomentImg;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.UI.MyCircleImageView;
import com.example.city_home_app.UI.UIUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Adapter_Moment extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private FragmentManager fragmentManager;
    private List<Down_Moment> list;
    private List<Integer> flag;

    public Adapter_Moment(Context context, FragmentManager fragmentManager) {
        this.context = context;
        this.fragmentManager = fragmentManager;
    }

    public void setList(List<Down_Moment> list) {
        this.list = list;
        flag = new ArrayList<>();
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.recitem_moment, parent, false);
        Adapter_Moment.ViewHolder viewHolder = new Adapter_Moment.ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final Down_Moment bean = list.get(position);
        if (bean == null) {
            return;
        }
        final Adapter_Moment.ViewHolder viewHolder = (Adapter_Moment.ViewHolder) holder;

        //判断是否是用户的动态
        if(BaseActivity.getUserinfo().getUserid()==bean.getUser_id()){
            viewHolder.imgRecitemMomentdel.setVisibility(View.VISIBLE);
        }else{
            viewHolder.imgRecitemMomentdel.setVisibility(View.GONE);
        }

        RetrofitListener<Down_Universe> islikelistener = new RetrofitListener<Down_Universe>() {
            @Override
            public void onResponse(Down_Universe listener) {
                if (listener.getMessage().equals("success")) {
                    flag.add(1);
                    viewHolder.imgRecitemMomentLike.setImageResource(R.drawable.ic_like);
                } else {
                    flag.add(0);
                    viewHolder.imgRecitemMomentLike.setImageResource(R.drawable.ic_like_no);
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };

        final RetrofitListener<Down_Universe> unlikelistener = new RetrofitListener<Down_Universe>() {
            @Override
            public void onResponse(Down_Universe listener) {
                if (listener.getMessage().equals("success")) {
                    flag.set(position, 0);
                    viewHolder.imgRecitemMomentLike.setImageResource(R.drawable.ic_like_no);
                    if (bean.getMoment_like() == 1) {
                        viewHolder.tvRecitemMomentLikecount.setVisibility(View.INVISIBLE);
                        list.get(position).setMoment_like(0);
                    } else {
                        viewHolder.tvRecitemMomentLikecount.setVisibility(View.VISIBLE);
                        int like = bean.getMoment_like();
                        list.get(position).setMoment_like(like - 1);
                        viewHolder.tvRecitemMomentLikecount.setText(list.get(position).getMoment_like() + "");
                    }

                }
            }

            @Override
            public void onFail(String msg) {

            }
        };

        final RetrofitListener<Down_Universe> likelistener = new RetrofitListener<Down_Universe>() {
            @Override
            public void onResponse(Down_Universe listener) {
                if (listener.getMessage().equals("success")) {
                    flag.set(position, 1);
                    viewHolder.imgRecitemMomentLike.setImageResource(R.drawable.ic_like);
                    if (bean.getMoment_like() == 0) {
                        viewHolder.tvRecitemMomentLikecount.setVisibility(View.VISIBLE);
                    }
                    int like = list.get(position).getMoment_like();
                    list.get(position).setMoment_like(like + 1);
                    viewHolder.tvRecitemMomentLikecount.setText(list.get(position).getMoment_like() + "");
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };

        final RetrofitListener<Down_Universe> dellistener = new RetrofitListener<Down_Universe>() {
            @Override
            public void onResponse(Down_Universe listener) {
                list.remove(position);
                notifyDataSetChanged();
            }

            @Override
            public void onFail(String msg) {

            }
        };

        viewHolder.tvRecitemMomentinfoAuthor.setText(bean.getUser_name());
        viewHolder.tvRecitemMomentinfoContent.setText(bean.getMoment_content());
        if (bean.getMoment_like() == 0) {
            viewHolder.tvRecitemMomentLikecount.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.tvRecitemMomentLikecount.setVisibility(View.VISIBLE);
        }
        viewHolder.tvRecitemMomentLikecount.setText(bean.getMoment_like() + "");
        viewHolder.tvRecitemMomentinfoTime.setText(UIUtils.removeTime(bean.getMoment_time()));
        Picasso.with(context)
                .load("http://siminzhang.top:8088/city_home_server/public/static/posters/" + bean.getUser_headimage())
                .into(viewHolder.imgRecitemMomentHead);
        //解析图片 如果有视频，则列表只有一张图片
        List<MomentImg> momentImgs = new ArrayList<>();
        getimgs(bean, momentImgs);
        //如果有video则只显示video
        if (!bean.getMoment_video().equals("")) {
            /*MomentImg b = new MomentImg();
            b.setType(2);
            b.setUrl(bean.getMoment_video());
            momentImgs.add(b);*/
            viewHolder.recRecitemMomentinfo.setVisibility(View.GONE);
            viewHolder.frameRecitemMomentVideo.setVisibility(View.VISIBLE);
            Picasso.with(context)
                    .load("http://siminzhang.top:8088/city_home_server/public/static/posters/" + momentImgs.get(0).getUrl())
                    .into(viewHolder.imgRecitemMomentVideo);
        } else {
            viewHolder.recRecitemMomentinfo.setVisibility(View.VISIBLE);
            viewHolder.frameRecitemMomentVideo.setVisibility(View.GONE);
            //显示图片rec
            if (!(momentImgs.size() == 1 && momentImgs.get(0).getUrl().equals(""))) {
                Adapter_MomentImg adapter_momentImg;
                viewHolder.recRecitemMomentinfo.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
                adapter_momentImg = new Adapter_MomentImg(context);
                viewHolder.recRecitemMomentinfo.setAdapter(adapter_momentImg);
                viewHolder.recRecitemMomentinfo.setItemAnimator(new DefaultItemAnimator());
                adapter_momentImg.setList(momentImgs);
                adapter_momentImg.notifyDataSetChanged();
            }
        }

        final ApiModel model = new ApiModel();
        model.islikemoment(bean.getMoment_id(), BaseActivity.getUserinfo().getUserid(), islikelistener);

        viewHolder.imgRecitemMomentLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag.get(position) == 1) {
                    model.unlikemoment(bean.getMoment_id(), BaseActivity.getUserinfo().getUserid(), unlikelistener);
                } else {
                    model.likemoment(bean.getMoment_id(), BaseActivity.getUserinfo().getUserid(), likelistener);
                }
            }
        });

        viewHolder.layoutRecitemMoment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MomentInfoActivity.class);
                intent.putExtra("id", bean.getMoment_id());
                context.startActivity(intent);
            }
        });

        viewHolder.recRecitemMomentinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MomentInfoActivity.class);
                intent.putExtra("id", bean.getMoment_id());
                context.startActivity(intent);
            }
        });

        viewHolder.imgRecitemMomentVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, VideoActivity.class);
                intent.putExtra("url", bean.getMoment_video());
                intent.putExtra("title", bean.getUser_name() + " 的上传视频");
                context.startActivity(intent);
            }
        });


        viewHolder.imgRecitemMomentHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, UserInfoActivity.class);
                intent.putExtra("id", bean.getUser_id());
                context.startActivity(intent);
            }
        });

        viewHolder.imgRecitemMomentdel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog.Builder normalDialog =
                        new AlertDialog.Builder(context);
                normalDialog.setTitle("删除");
                normalDialog.setMessage("是否删除该动态");
                normalDialog.setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                model.delmoment(bean.getMoment_id(),dellistener);
                            }
                        });
                normalDialog.setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                // 显示
                normalDialog.show();
            }
        });
    }


    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    private void getimgs(Down_Moment bean, List<MomentImg> list) {
        String s[] = bean.getMoment_pic().split(",");
        for (int i = 0; i < s.length; i++) {
            MomentImg b = new MomentImg();
            b.setType(1);
            b.setUrl(s[i]);
            list.add(b);
        }
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_recitem_moment_head)
        MyCircleImageView imgRecitemMomentHead;
        @BindView(R.id.tv_recitem_momentinfo_author)
        TextView tvRecitemMomentinfoAuthor;
        @BindView(R.id.tv_recitem_momentinfo_time)
        TextView tvRecitemMomentinfoTime;
        @BindView(R.id.tv_recitem_momentinfo_content)
        TextView tvRecitemMomentinfoContent;
        @BindView(R.id.rec_recitem_momentinfo)
        RecyclerView recRecitemMomentinfo;
        @BindView(R.id.img_recitem_moment_like)
        ImageView imgRecitemMomentLike;
        @BindView(R.id.img_recitem_moment_comment)
        ImageView imgRecitemMomentComment;
        @BindView(R.id.tv_recitem_moment_likecount)
        TextView tvRecitemMomentLikecount;
        @BindView(R.id.layout_recitem_moment)
        ConstraintLayout layoutRecitemMoment;
        @BindView(R.id.linearLayout7)
        LinearLayout linearLayout7;
        @BindView(R.id.img_recitem_moment_video)
        ImageView imgRecitemMomentVideo;
        @BindView(R.id.constraintLayout)
        ConstraintLayout constraintLayout;
        @BindView(R.id.frame_recitem_moment_video)
        FrameLayout frameRecitemMomentVideo;
        @BindView(R.id.img_recitem_momentdel)
        ImageView imgRecitemMomentdel;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
